﻿using Studio77.PopBall.Entities;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;

namespace Studio77.PopBall.Core
{
    public class StageManager
    {
        public static StageManager Instance = new StageManager();

        public bool TurnMoka = true;

        /// <summary>
        /// 1-显示所有广告
        /// 2-仅显示微软广告
        /// </summary>
        public int AdType = 1;

        public bool isLoadAdType = false;

        //keys
        private const string LAST_STAGE_INDEX_KEY = "LastStageKey";
        private const string IS_PLAY_SOUND_KEY = "isPlaySoundKey";
        private const string HIGH_SCORE_KEY = "HighScoreKey";
        private const string SAVE_DATA = "SaveDataKey";
        private const string MAGIC_BALL_COUNT = "MagicBallCount";

        private const string LAUNCH_COUNT = "launchCount";
        private const string MARKET_COMMENT = "marketComment";
        private const string NEW_USER_TASK_KEY = "newUserTaskKey";

        private const string LAST_FULLSCREEN_DATE_KEY = "LAST_FULLSCREEN_DATE";

        private StageManager()
        {

        }


        public void Init()
        {
            InitHistoryData();
        }

        private string lastFullScreenDate = "XXXXXX";
        /// <summary>
        /// 上一次显示全屏广告的日期
        /// </summary>
        public string LastFullScreenDate
        {
            get
            {
                return lastFullScreenDate;
            }
            set
            {
                lastFullScreenDate = value;
                IsolatedStorageSettings.ApplicationSettings[LAST_FULLSCREEN_DATE_KEY] = lastFullScreenDate;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int magicBallCount = 0;
        public int MagicBallCount
        {
            get
            {
                return magicBallCount;
            }
            set
            {
                magicBallCount = value;
                IsolatedStorageSettings.ApplicationSettings[MAGIC_BALL_COUNT] = magicBallCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int launchCount = 1;
        public int LaunchCount
        {
            get
            {
                return launchCount;
            }
            set
            {
                launchCount = value;
                IsolatedStorageSettings.ApplicationSettings[LAUNCH_COUNT] = launchCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isDoNewUserTask = false;
        public bool IsDoNewUserTask
        {
            get
            {
                return isDoNewUserTask;
            }
            set
            {
                isDoNewUserTask = value;
                IsolatedStorageSettings.ApplicationSettings[NEW_USER_TASK_KEY] = isDoNewUserTask;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isCommented = false;
        public bool IsCommented
        {
            get
            {
                return isCommented;
            }
            set
            {
                isCommented = value;
                IsolatedStorageSettings.ApplicationSettings[MARKET_COMMENT] = isCommented;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isPlaySound = true;

        /// <summary>
        /// 是否播放音效
        /// </summary>
        public bool IsPlaySound
        {
            get
            {
                return isPlaySound;
            }
            set
            {
                isPlaySound = value;
                IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY] = isPlaySound;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int lastStage;
        /// <summary>
        /// 上一个关卡
        /// </summary>
        public int LastStage
        {
            get
            {
                return lastStage;
            }
            set
            {
                lastStage = value;
                IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_INDEX_KEY] = lastStage;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int highScore;
        /// <summary>
        /// 最高分
        /// </summary>
        public int HighScore
        {
            get
            {
                return highScore;
            }
            set
            {
                highScore = value;
                IsolatedStorageSettings.ApplicationSettings[HIGH_SCORE_KEY] = highScore;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private SavedGameData saveData;
        /// <summary>
        /// 最高分
        /// </summary>
        public SavedGameData SaveData
        {
            get
            {
                return saveData;
            }
            set
            {
                saveData = value;
                IsolatedStorageSettings.ApplicationSettings[SAVE_DATA] = saveData;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private void InitHistoryData()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAUNCH_COUNT))
            {
                launchCount = (int)IsolatedStorageSettings.ApplicationSettings[LAUNCH_COUNT];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(MARKET_COMMENT))
            {
                isCommented = (bool)IsolatedStorageSettings.ApplicationSettings[MARKET_COMMENT];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(NEW_USER_TASK_KEY))
            {
                isDoNewUserTask = (bool)IsolatedStorageSettings.ApplicationSettings[NEW_USER_TASK_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(MAGIC_BALL_COUNT))
            {
                magicBallCount = (int)IsolatedStorageSettings.ApplicationSettings[MAGIC_BALL_COUNT];
            }
            else
            {
                magicBallCount = 0;
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(HIGH_SCORE_KEY))
            {
                highScore = (int)IsolatedStorageSettings.ApplicationSettings[HIGH_SCORE_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(SAVE_DATA) && IsolatedStorageSettings.ApplicationSettings[SAVE_DATA] != null)
            {
                saveData = (SavedGameData)IsolatedStorageSettings.ApplicationSettings[SAVE_DATA];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAST_STAGE_INDEX_KEY) && IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_INDEX_KEY] != null)
            {
                lastStage = (int)IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_INDEX_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_PLAY_SOUND_KEY) && IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY] != null)
            {
                isPlaySound = (bool)IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAST_FULLSCREEN_DATE_KEY))
            {
                lastFullScreenDate = (string)IsolatedStorageSettings.ApplicationSettings[LAST_FULLSCREEN_DATE_KEY];
            }
        }

        //public void SaveModeUnlockInfo()
        //{
        //    IsolatedStorageSettings.ApplicationSettings[MODE_UNLOCK_STATUS_KEY] = ModeUnlockStatus;
        //    IsolatedStorageSettings.ApplicationSettings.Save();
        //}

        //public void SaveStageUnlockInfo()
        //{
        //    IsolatedStorageSettings.ApplicationSettings[STAGE_UNLOCK_STATUS_KEY] = StageUnlockStatus;
        //    IsolatedStorageSettings.ApplicationSettings.Save();
        //}

    }
}
